package app.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import app.domain.AdminAssess;
import app.domain.Assess;
import app.domain.FinishedOrder;
import app.domain.UserInfo;
import app.service.CommentService;
import app.service.WorkManageService;


@Controller
public class CommentController {
	public static Logger logger=LogManager.getLogger(CommentController.class);
	@Autowired
	private CommentService commentService;
	@Autowired
	private WorkManageService workManageService;
	@RequestMapping("/showFinishedOrder")
	public String showFinishedOrder(HttpServletRequest request){
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
		Integer accountId = userInfo.getAccountId();
		List<FinishedOrder> finishedOrders = commentService.showFinishedOrder(accountId);
		for(int i = 0;i<finishedOrders.size();i++){
			Assess assess = commentService.findAssess(finishedOrders.get(i).getId());
			if(assess!=null){
				finishedOrders.get(i).setJudge("true");
			}else{
				finishedOrders.get(i).setJudge("false");
			}
			System.out.println(finishedOrders.get(i).toString());
		}
		request.setAttribute("orders", finishedOrders);

		return "user/finishOrder";
	}
	
	@RequestMapping("/Assess")
	public String assess(Assess assess,HttpServletRequest request){
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
		Integer accountId = userInfo.getAccountId();
		commentService.assess(assess);
		
		List<FinishedOrder> finishedOrders = commentService.showFinishedOrder(accountId);
		for(int i = 0;i<finishedOrders.size();i++){
			Assess assess2 = commentService.findAssess(finishedOrders.get(i).getId());
			if(assess2!=null){
				finishedOrders.get(i).setJudge("true");
			}else{
				finishedOrders.get(i).setJudge("false");
			}
		}
		request.setAttribute("orders", finishedOrders);
		return "user/finishOrder";
	}
	
	@RequestMapping("/findAllAssess")
	public String findAllAssess(HttpServletRequest request){
		//UserInfo userInfo = (UserInfo) request.getSession().getAttribute("userInfo");
		List<Assess> assessList = commentService.findAllAssess();
		List<AdminAssess> adminAssesses = new ArrayList<>();
		for (int i = 0; i < assessList.size(); i++) {
			String workerName = workManageService.findNameByOrderId(assessList.get(i).getOrderId());
			AdminAssess adminAssess = new AdminAssess();
			adminAssess.setWorkName(workerName);
			adminAssess.setOrderId(assessList.get(i).getOrderId());
			adminAssess.setRank(assessList.get(i).getRank());
			adminAssess.setComment(assessList.get(i).getComment());
			adminAssesses.add(adminAssess);
			System.out.println(adminAssess.toString());
		}
		request.setAttribute("adminAssess", adminAssesses);
		return "admin/assessList";
	}
}
